package com.disney.dolcn.invitemail.util;

import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.disney.dolcn.core.database.DatabaseEngine;
import com.disney.dolcn.invitemail.bean.SendMailObject;
import com.disney.dolcn.invitemail.logic.InviteLogic;
import com.disney.dolcn.invitemail.logic.InviteLogicImpl;

/**
 * 
 * @author hawang
 * 
 */
public class TaskTime {
	Timer timer;

	long hour = 10000;// /间隔一小时

	public static TaskTime ts = new TaskTime();

	/**
	 * 
	 * @return
	 */
	public static TaskTime getInstanst() {
		if (ts == null)
			ts = new TaskTime();
		return ts;
	}

	/**
	 * 
	 */
	public void BeginTaskTime() {
		timer = new Timer();
		timer.scheduleAtFixedRate(new RemindTask(), 1000 * 10, hour);
	}

	/**
	 * 邮件提取进程
	 * 
	 * @author hawang
	 * 
	 */
	class RemindTask extends TimerTask {
		public void run() {
			try {
				System.out.println(" RemindTask 邮件提取进程启动");
				InviteLogic logic = new InviteLogicImpl(DatabaseEngine
						.getInstance().createTemplate(
								InviteConstant.DB_SECTION_NAME));
				List list = logic.getInviteEmail();
				System.out.println("run===" + list.size());
				if (list == null)
					return;

				if (list.size() > 0) {
					String ids = addMailList(list);// 向发送队列添加新邮件
					System.out.println("ids ===" + ids);
				}
			} catch (Exception s) {
			}

		}
	}

	/**
	 * 邮件发送第一进入方法
	 * 
	 * @param sendmail
	 */
	public void sendEmai(SendMailObject sendmail) {
		String OLD_STR[] = { "##member.name##", "##member.email##" };
		String NEW_STR[] = { sendmail.getName(), sendmail.getEmail() };
		MailService ms = new MailService("10.0.0.48", "disney-edm@disney.cn",
				"disney-edm@disney.cn", "Dfkq_BApvw");
		String context = InviteConstant.CONTEXT;
		System.out.println("发送前邮件内容长度" + context.length());
		try {
			context = ContextUtil.prepareTemplate(context, OLD_STR, NEW_STR);
			ms.sendMail(sendmail.getInvite_email(), "您的朋友" + sendmail.getName()
					+ "推荐您观看迪士尼精彩视频", context, "迪士尼中国官网");

			// 邮件发送失败
		} catch (Exception d) {
			d.printStackTrace();
		}

	}

	/**
	 * 给动态邮件列表添加内容，操作前已经停止发送
	 * 
	 * @param list
	 * @return
	 */
	private String addMailList(List list) {
		InviteLogic logic = new InviteLogicImpl(DatabaseEngine.getInstance()
				.createTemplate(InviteConstant.DB_SECTION_NAME));
		String id = "";
		for (int i = 0; i < list.size(); i++) {
			HashMap t = (HashMap) list.get(i);
			System.out.println("HashMap" + t);
			if (t == null)
				continue;
			id += "," + t.get("id");
			String name = (String) t.get("name");
			String email = (String) t.get("email");
			String emails[] = ((String) t.get("intive_emails")).split(";");

			for (int b = 0; b < emails.length; b++) {
				try {
					if (logic.getListUser(name, emails[b].trim()) < 1) {
						System.out.println("sendEmai前" + emails[b].trim());
						sendEmai(new SendMailObject(name, email, emails[b]
								.trim()));
					}
				} catch (Exception d) {
					d.printStackTrace();
				}
			}
			if ((i + 1) % 20 == 0)// 每处理20条记录，修改一次已发送状态。
			{
				logic.setInviteEmail(id);// 修改数据库发送状态
				id = "";
			}

		}
		logic.setInviteEmail(id);// 修改数据库发送状态
		return id;
	}

	public static void main(String sendmail[]) {
		try {
			MailService ms = new MailService("mail.51gcs.com", "hr@51gcs.com",
					"hr@51gcs.com", "silitech");
			ms.sendMail("80016144@qq.com", "您的朋友推荐您观看迪士尼精彩视频", "sdsds",
					"迪士尼中国官网");

		} catch (Exception d) {
			d.printStackTrace();
		}
	}

}
